//248:https://www.luogu.com.cn/problem/P3146


#include <iostream>

using namespace std;

const int N = 260;

int a[N];
int f[N][N];

int main()
{
	int n;
	cin >> n;
	int ret = 0;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
		ret = max(ret,a[i]);
		f[i][i] = a[i];
	}
	
	for(int len = 2; len <= n; len++)
	{
		for(int i = 1; i+len-1 <= n; i++)
		{
			int j = i + len - 1;
			
			for(int k = i; k < j; k++)
			{
				if(f[i][k] && f[i][k] == f[k+1][j])
				{
					f[i][j] = max(f[i][j],f[i][k]+1);
				}
			}
			ret = max(f[i][j],ret);
		}
	}
	cout << ret;
	return 0;
}
